package com.ruoyi.web.controller.config;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.entity.LawRegulationsStandard;
import com.ruoyi.system.service.LawRegulationsStandardService;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 法律法规标准(LawRegulationsStandard)表控制层
 * @author makejava
 * @since 2023-03-14 10:55:03
 */
@RestController
@RequestMapping("lawRegulationsStandard")
public class LawRegulationsStandardController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private LawRegulationsStandardService lawRegulationsStandardService;

    @GetMapping("/list")
    public TableDataInfo queryByPage(LawRegulationsStandard lawRegulationsStandard) {
        startPage();
        List<LawRegulationsStandard> list = lawRegulationsStandardService.queryAll(lawRegulationsStandard);
        return getDataTable(list);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<LawRegulationsStandard> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.lawRegulationsStandardService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param lawRegulationsStandard 实体
     * @return 新增结果
     */
    @PostMapping("add")
    public ResponseEntity<LawRegulationsStandard> add(@Validated @RequestBody LawRegulationsStandard lawRegulationsStandard) {
        return ResponseEntity.ok(this.lawRegulationsStandardService.insert(lawRegulationsStandard));
    }

    /**
     * 编辑数据
     *
     * @param lawRegulationsStandard 实体
     * @return 编辑结果
     */
    @PutMapping("edit")
    public ResponseEntity<LawRegulationsStandard> edit(@Validated @RequestBody LawRegulationsStandard lawRegulationsStandard) {
        return ResponseEntity.ok(this.lawRegulationsStandardService.update(lawRegulationsStandard));
    }

    /**
     * 删除数据
     *
     * @param lawRegulationsStandard 主键
     * @return 删除是否成功
     */
    @DeleteMapping("delete")
    public ResponseEntity<Boolean> deleteById(@Validated @RequestBody LawRegulationsStandard lawRegulationsStandard) {
        return ResponseEntity.ok(this.lawRegulationsStandardService.deleteById(lawRegulationsStandard.getId()));
    }

    @Log(title = "法律法规标准", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<LawRegulationsStandard> util = new ExcelUtil<>(LawRegulationsStandard.class);
        List<LawRegulationsStandard> lawRegulationsStandards = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = lawRegulationsStandardService.importExcel(lawRegulationsStandards, updateSupport, operName);
        return success(message);
    }


    @Log(title = "法律法规标准", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LawRegulationsStandard lawRegulationsStandard) {
        List<LawRegulationsStandard> list = lawRegulationsStandardService.queryAll(lawRegulationsStandard);
        ExcelUtil<LawRegulationsStandard> util = new ExcelUtil<>(LawRegulationsStandard.class);
        util.exportExcel(response, list, "法律法规标准");
    }


    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<LawRegulationsStandard> util = new ExcelUtil<>(LawRegulationsStandard.class);
        util.importTemplateExcel(response, "法律法规标准");
    }


}

